package com.tigercel.xiangchengyun.adapter

import android.content.Context
import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.entity.ServiceEntity
import com.tigercel.xiangchengyun.ui.activities.LivePlayActivity
import com.tigercel.xiangchengyun.ui.activities.WebViewActivity

import java.util.*

/**
 * Created by wqj on 2018/5/31.
 */
class ServiceListAdapter(private val mContext: Context, lists: MutableList<List<ServiceEntity>>?, titlesList: ArrayList<String>) : BaseAdapter() {
    private var mLists: MutableList<List<ServiceEntity>>? = null

    private var mTitlesList = ArrayList<String>()

    init {
        mLists = lists
        mTitlesList = titlesList
    }


    override fun getCount(): Int {
        return mTitlesList.size
    }

    override fun getItem(position: Int): Any {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val viewHolder: ViewHolder
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.services, null)
            viewHolder = ViewHolder()
            viewHolder.title = convertView!!.findViewById(R.id.service_title)
            viewHolder.mRecyclerView = convertView.findViewById(R.id.service_recyclerview)
            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }
        viewHolder.title!!.text = mTitlesList[position]

        val servicesRecyclerViewAdapter = ServicesRecyclerViewAdapter(R.layout.item_service, mLists!![position])
        val gridLayoutManager = GridLayoutManager(mContext, 2)
        viewHolder.mRecyclerView!!.layoutManager = gridLayoutManager
        viewHolder.mRecyclerView!!.adapter = servicesRecyclerViewAdapter
        servicesRecyclerViewAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position1 ->
            if (mLists!![position][position1].service_name == "广电直播") {
                var intent = Intent()
                intent.setClass(mContext, LivePlayActivity::class.java)
                mContext.startActivity(intent)
            } else {
                var intent = Intent()
                intent.putExtra("title", mLists!![position][position1].service_name)
                intent.putExtra("url", mLists!![position][position1].url)
                intent.putExtra("id", "")
                intent.setClass(mContext, WebViewActivity::class.java)
                mContext.startActivity(intent)
            }
        }
        return convertView
    }

    internal inner class ViewHolder {
        var title: TextView? = null
        var mRecyclerView: RecyclerView? = null
    }
}
